<?php


namespace app\shop\controller\group;


use app\BaseController;
use app\shop\common\consts\Consts;
use app\shop\service\chat\ChatUserService;
use think\facade\Cache;
use think\facade\Db;

class GroupController extends BaseController
{

    /**
     * 群成员
     * @return \think\response\Json
     */
    public function groupUserArr()
    {
        try {
            $chatSign = $this->request->param('chat_sign', '', 'trim');
            $service = new ChatUserService();
            $list = $service->groupUserArr($chatSign);
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 群信息
     * @return \think\response\Json
     */
    public function getGroupInfo()
    {
        try {
            $chatSign = $this->request->param('chat_sign', '', 'trim');
            $service = new ChatUserService();
            $list = $service->getGroupInfo($chatSign);
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 邀请用户加入群聊
     * @return \think\response\Json
     */
    public function joinGroup()
    {
        try {
            $chatSign = $this->request->param('chat_sign', '', 'trim');
            $userInfo = $this->request->middleware('userInfo');
            $mobile = $this->request->param('mobile', '', 'trim');
            $roleId = $userInfo['role_id'];
            if ($roleId != Consts::SERVICE_LEADER_ID && $roleId != Consts::CUSTOMER_ID) {
                throw new \Exception('您没有加人的权限');
            }
            $service = new ChatUserService();
            $service->joinGroup($chatSign, $mobile);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 修改群聊名称
     * @return \think\response\Json
     */
    public function modifyGroupTitle()
    {
        try {
            $chatSign = $this->request->param('chat_sign', '', 'trim');
            $userInfo = $this->request->middleware('userInfo');
            $title = $this->request->param('title', '', 'trim');
            if ($title == '') {
                throw new \Exception('请填写群名');
            }
            $service = new ChatUserService();
            $service->modifyGroupTitle($chatSign, $userInfo, $title);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 修改群聊公告
     * @return \think\response\Json
     */
    public function modifyGroupNotice()
    {
        try {
            $chatSign = $this->request->param('chat_sign', '', 'trim');
            $userInfo = $this->request->middleware('userInfo');
            $notice = $this->request->param('notice', '', 'trim');

            $service = new ChatUserService();
            $service->modifyGroupArticle($chatSign, $userInfo, $notice);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 移出群聊
     * @return \think\response\Json
     */
    public function kickOutGroup()
    {
        Db::startTrans();
        try {
            $chatSign = $this->request->param('chat_sign', '', 'trim');
            $userInfo = $this->request->middleware('userInfo');
            $uids = $this->request->param('uids', '', 'trim');
            $roleId = $userInfo['role_id'];
            if ($roleId != Consts::SERVICE_LEADER_ID) {
                throw new \Exception('您没有T人的权限');
            }
            if (in_array($userInfo['user_id'], $uids)) {
                throw new \Exception('非法操作');
            }
            $service = new ChatUserService();
            $service->kickOutGroup($chatSign, $uids);
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollBack();
            return $this->error($exception->getMessage().$exception->getLine());
        }
    }

    /**
     * 解散群聊
     * @return \think\response\Json
     */
    public function dissolveGroup()
    {
        Db::startTrans();
        try {
            $chatSign = $this->request->param('chat_sign', '', 'trim');
            $userInfo = $this->request->middleware('userInfo');
            $roleId = $userInfo['role_id'];
            if ($roleId != Consts::SERVICE_LEADER_ID) {
                throw new \Exception('您没有解散群聊的权限');
            }
            $service = new ChatUserService();
            $service->dissolveGroup($chatSign);
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollBack();
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 获取公告是否已读
     * @return \think\response\Json
     */
    public function getNoticeIsRead()
    {
        try {
            $chatSign = $this->request->param('chat_sign', '', 'trim');
            $noticeId = $this->request->param('notice_id', '', 'trim');
            $userId = $this->request->middleware('userId');

            $key = sprintf($chatSign . ":" . $noticeId);
            $redis = Cache::store('redis')->handler();
            $result = $redis->SISMEMBER($key, $userId);
            return $this->success($result);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 设置公告已读
     * @return \think\response\Json
     */
    public function setNoticeIsRead()
    {
        try {
            $chatSign = $this->request->param('chat_sign', '', 'trim');
            $noticeId = $this->request->param('notice_id', '', 'trim');
            $userId = $this->request->middleware('userId');

            $key = sprintf($chatSign . ":" . $noticeId);
            $redis = Cache::store('redis')->handler();
            $redis->SADD($key, $userId);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}